import axios from 'axios'
import Vue from 'vue'
const vm = Vue.prototype
let BASE_ULR = '' // 测试环境 走proxy
let isProduction = true
if (isProduction) {
  BASE_ULR = 'https://jifen.jinshanju.com' // 生产环境
} else {
  BASE_ULR = 'http://jifentest.jinshanju.com' // 测试环境
}

const service = axios.create({
  // baseURL: process.env.API_BASEURL,
  baseURL: BASE_ULR, // api 的 base_url
  timeout: 5000, // request timeout
})
const SELLER = 'seller'
const GOODSID = 'goodsid'
const PLATESOURCE = 'platesource'
// request interceptor
service.interceptors.request.use(
  config => {

    // 每个接口都穿seller参数
    const seller = vm.$getSessionItem(SELLER)
    if (seller) {
      config.headers[SELLER] = seller
    }
    // 每个接口都穿platesource参数
    const platesource = vm.$getSessionItem(PLATESOURCE)
    if (platesource) {
      config.headers[PLATESOURCE] = platesource
    }
    // 如果是非授权接口才传goodsid参数
    if (!config.url.includes('/changyou/queryauth')) {
      const goodsid = vm.$getSessionItem(GOODSID)
      if (goodsid) {
        config.headers[GOODSID] = goodsid
      }
    }
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    return response
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service
